<?php
namespace app\api\controller\goods;
use app\api\controller\Init;

class Goods extends Init
{
    public function initialize() {
        parent::initialize();
        $this->service = model('goods/Goods', 'service');
    }

    /**
     * 产品列表
     * @return array
     *
     * @author <xiachao>
     * @date   2018/11/23 10:27
     */
    public function lists()
    {
        $field = $this->request->param('field/s',true);
        $relations = array_filter($this->request->param('relations/a',[]));
        $attrs = array_filter($this->request->param('attrs/a',[]));
        $maps = [];
        $maps['site_id'] = $this->site['id'];
        $maps['current_id'] = $this->site['custom_id'];
        $maps['status'] = 1;
        if ($this->request->has('keywords', 'param', true)) {
            $maps['name'] = $this->request->param('keywords/s', '');
        }
        $result = $this->service->lists($maps,'sort asc',$this->page,$this->limit,$field,$relations,$attrs);
        if (!$result) {
            return $this->response($this->service->getError(),[],-100001);
        }
        return $this->response("success", $result);
    }

    /**
     * 详情
     * @return array
     *
     * @author <xiachao>
     * @date   2018/11/23 11:27
     */
    public function detail() {
        $maps = [];
        $maps['goods_id'] = $this->request->param('goods_id/s','');
        if (empty($maps['goods_id'])) {
            return $this->response('产品信息不能为空', [], -300001);
        }
        $field = $this->request->param('field/s',true);
        $relations = array_filter($this->request->post('relations/a',[]));
        $attrs = array_filter($this->request->post('attrs/a',[]));
        $maps['site_id'] = $this->site['id'];
        $maps['current_id'] = $this->site['custom_id'];
        $maps['status'] = 1;
        $result = $this->service->detail($maps,$field,$relations,$attrs);
        if (!$result) {
            return $this->response($this->service->getError(),[],-100003);
        }
        return $this->response("success", $result);
    }

}